Repository navigation
Let iOS JS timers sleep until they are due - #59317
Open
hryhoriiK97 wants to merge 2 commits into
Open
hryhoriiK97 wants to merge 2 commits into
hryhoriiK97 wants to merge 2 commits into
Conversation
`RCTTiming` keeps the JS thread's display link running while any timer is due within a second, which wakes the JS thread on every frame until then. A `setInterval` of up to a second keeps it waking at the frame rate for as long as it exists. Behind the new `sleepUntilNextTimerIOS` feature flag (default `false`), the display link only runs for timers due within the next two frames, and the existing sleep timer waits for timers further away. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Regenerate the feature flag files with `yarn featureflags`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
Author
|
@cipolleschi @javache This lowers the threshold of |
Contributor
Author
|
@javache @cipolleschi Is lowering the sleep threshold behind a flag something you'd accept? If yes, I'll rebase it on main. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
On iOS,
RCTTiming(React/CoreModules/RCTTiming.mm) fires JS timers from a display link on the JS thread. While a timer is pending and due withinkMinimumSleepInterval(1 second), the display link keeps running and wakes the JS thread on every frame until the timer is due. Timers further away use a one-shotNSTimer(the sleep timer) instead.Current behavior. Any timer under one second keeps the JS thread waking at the display's frame rate. A single
setInterval(fn, 1000)is never more than a second away, so it keeps it waking 60 times per second (120 on ProMotion displays with high frame rates enabled) for as long as the interval exists, even when nothing else happens. Countdowns, clocks, polling, debounced inputs and library timers all do this.With this change. Behind the new
sleepUntilNextTimerIOSfeature flag (defaultfalse), the display link only runs for timers due within the next two frames. A timer further away is waited for with the existing sleep timer, which already handles timers more than a second away, so the JS thread wakes when the timer is due instead of on every frame. Timers due within two frames,requestAnimationFrameincluded, still run from the display link. Timers further away now fire when they are due instead of at the next frame after it.Changelog:
[Internal]
Test Plan:
RCTTimingTests(new,RNTesterUnitTests, run byyarn test-ios): 8 tests for when the display link runs and when a sleep timer waits, with the flag on and off; that a sleeping timer is called when due and not before; that a nearer timer moves the sleep timer earlier; that a deleted sleeping timer isn't called; and that the display link pauses once the next timer is further than two frames away. They pass on an iPhone simulator, three runs in a row; with the threshold change disabled, the 4 that need it fail.clang-formatpasses on the changed files, andyarn featureflags --verify-unchangedpasses.xcodebuildin Release and run on a physical iPhone 15 Pro (iOS 26.6.1), on an otherwise idle screen. The two arms are the same binary with the flag switched by a launch environment variable, in the order before, after, after, before, 4 minutes per launch. Counted: display link callbacks and sleep timer wakeups on the JS thread, the JS thread's CPU time (thread_info), the app's CPU time and context switches (getrusage).setInterval(() => {}, 1000): JS thread wakeupssetTimeouts with random delays of 0–1.5 s, timed in JS withperformance.now(): none fired early, out of order or not at all, in both arms. Median lateness 10.1 → 8.8 ms, 95th percentile 16.9 → 16.6 ms. Timers of 50–500 ms now fire about 1 ms after they are due instead of at the next frame (up to 16.7 ms later). A timer due within two frames that starts while the display link is paused waits for it to restart, as it does today when no other timer is pending; in the timer chain above, the 0 ms and 16 ms timers fired 14.2 ms late on average instead of 8.6 ms, because the display link was paused more often.🤖 Generated with Claude Code